package com.telit.firecommand.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.beust.jcommander.internal.Maps;
import com.telit.common.core.utils.FeignUtils;
import com.telit.common.enums.ResultCode;
import com.telit.common.exception.ForestBaseException;
import com.telit.common.util.JwtTokenUtils;
import com.telit.common.util.LanguageUtils;
import com.telit.upms.feign.RemoteUpmsService;
import com.telit.upms.vo.MicroSysUserAllInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @Auther amy
 * @Describe 用户信息获取业务层
 */
@Service
public class UserInfoService {


    @Autowired
    private RemoteUpmsService remoteUpmsService;

    public String getDistrictCode() {
        Long id = JwtTokenUtils.getId();
        MicroSysUserAllInfoVo userInfo = getUserInfo(id);
        return userInfo == null ? null : userInfo.getAreaCode();
    }

    public String getMobile() {
        Long id = JwtTokenUtils.getId();
        MicroSysUserAllInfoVo userInfo = getUserInfo(id);
        return userInfo == null ? null : userInfo.getMobile();
    }

    /**
     * 获取单个用户信息
     *
     * @return
     */
    public MicroSysUserAllInfoVo getUserInfo(Long id) {
        com.telit.common.core.utils.Result<Map<Long, MicroSysUserAllInfoVo>> mapResult = remoteUpmsService.selectUserContainDeptAndRoleWithBatchIds(
                Arrays.asList(id), Boolean.TRUE);
        Map<Long, MicroSysUserAllInfoVo> longMicroSysUserVoMap = FeignUtils.unwrap(mapResult);
        MicroSysUserAllInfoVo microSysUserAllInfoVo = longMicroSysUserVoMap.get(id);
        if (microSysUserAllInfoVo == null) {
            throw new ForestBaseException(LanguageUtils.codeMessage(ResultCode.OPERATE_ERROR.getCode(), id));
        }
        return microSysUserAllInfoVo;
    }


    public List<MicroSysUserAllInfoVo> queryUserInfoByIds(List<Long> recieverIdList) {

            com.telit.common.core.utils.Result<Map<Long, MicroSysUserAllInfoVo>> mapResult = remoteUpmsService.selectUserContainDeptAndRoleWithBatchIds(recieverIdList, Boolean.TRUE);

        Map<Long, MicroSysUserAllInfoVo> microSysUserVoMap = FeignUtils.unwrap(
                mapResult);

//        List<MicroSysUserAllInfoVo> microSysUserVos = recieverIdList.stream().map(
//                microSysUserVoMap::get).collect(Collectors.toList());
            List<MicroSysUserAllInfoVo> microSysUserVos = new ArrayList<MicroSysUserAllInfoVo> (microSysUserVoMap.values());

        return microSysUserVos;
    }



    /**
     * 根据传入的ids 获取 id 对应的用户名
     *
     * @param ids 集合id
     * @return 查询结果集
     */
    public Map<Long, String> findAndConvertUserInfoByIds(List<Long> ids) {
        final Map<Long, String> result = Maps.newHashMap();
        if (CollUtil.isEmpty(ids)) {
            return result;
        }
        final List<Long> queryParam = ids.parallelStream()
                //过滤掉空id
                .filter(ObjectUtil::isNotEmpty)
                //去重
                .distinct()
                .collect(Collectors.toList());
        if (CollUtil.isEmpty(queryParam)) {
            return result;
        }
        final List<MicroSysUserAllInfoVo> vos = queryUserInfoByIds(queryParam);
        if (CollUtil.isEmpty(vos)) {
            return result;
        }
        result.putAll(vos.stream()
                .collect(Collectors.toMap(MicroSysUserAllInfoVo::getId, MicroSysUserAllInfoVo::getRealName)));
        return result;
    }


}
